<?php

function enrichFromFile($obj, $path) {
    $source = readConfigFile($path);
    copyProperties($source, $obj);
}

function copyProperties($source, $target) {
    foreach(get_object_vars($source) as $prop => $value)
    {
        $target->$prop = $value;
    }
}



function readConfigFile($path) {
    $result = new StdClass;
    if (!file_exists($path)) {
        return $result;
    }
    
    $file = fopen($path, "r");


    $insideProp = false;
    $propName="";
    $propValue = "";

    while(!feof($file)){
        $line = fgets($file);
        $line = ltrim(rtrim($line));

        // comment and empty line check
        if ( (strlen($line) == 0) || strpos($line, "#") === 0 ) {
            continue;
        }

        // if there is no equal sign
        if (!$insideProp &&  strpos($line, "=") === false) {
            continue;
        }

        // if we are inside a multi-line property and it ends here
        if ($insideProp && endsWith($line, ">>")) {
            if (strlen($line) > 2) {
                $propValue .=  " " . substr($line, 0, strlen($line) - 2);
            }
            
            $result->$propName = $propValue;
            $insideProp = false;
            $propName = null;
            $propValue = null;
        }

        else if ($insideProp) { // if we are inside muli-line property
            $propValue .= " ".$line;
        }

        else { // base case

            $index = strpos($line, "=");

            $propName = ltrim(rtrim(substr($line, 0, $index)));
            $propValue = ltrim(rtrim(substr($line, $index + 1)));

            if (strlen($propValue) == 0 )  continue;

            if (strpos($propValue, "<<") === 0) { // multi-line property starts here
                $insideProp = true;
                $propValue = substr($propValue, 2);
            } else {
                // assign value
                $result->$propName = parseValue($propValue);
                $propName = null;
                $propValue = null;
            }


        }

    }

    fclose($file);
    return $result;
}

function getIconPath($root) {
     if (file_exists($root . "/icon.png")) {
        return $root . "/icon.png";
     } else if (file_exists($root . "/icon.jpg")) {
        return $root . "/icon.jpg";
     } else if (file_exists($root . "/icon.gif")) {
        return $root . "/icon.gif";
     } else {
         return "icon_not_found";
     }
}

// parse comma separated values into an array
function parseValue($str) {
    if (strpos($str, ",") === false) return $str;

    $arr = explode(",", $str);
    $result = array();
    foreach ($arr as $value) {
        $newValue = rtrim(ltrim($value));
        if (strlen($newValue) != 0) {
            array_push($result, $newValue);
        }
    }

    return $result;
}

function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }

    return (substr($haystack, -$length) === $needle);
}

function getFileNames($path) {
    if (!file_exists($path)) {
        throw new Exception("Directory was not found " . $path);
    }
    $arr = scandir($path);
    
    $result = array();
    foreach ($arr as $value) {
        if (strcmp(".", $value) == 0 || strcmp("..", $value) == 0 ) continue;
        array_push($result, $value);
    }
    return $result;
}

function parseFileNames($path) {
    $arr = getFileNames($path);
    $result = array();
    foreach ($arr as $value) {
        $element = new FileElement();
        $element->fileName = $value;

        $index = strpos($value, "_");
        if ($index !== false && is_numeric(substr($value, 0, $index))) {
            $element->name = substr($value, $index + 1);
        } else {
            $element->name = $value;
        }
        array_push($result, $element);
    }
    return $result;
}

class FileElement {
    public $name;
    public $fileName;
}


class Section {
    public $name;
    public $items = array();
}

?>


